
package huajiu.com.controller;

import huajiu.com.base.CdResult;
import huajiu.com.service.ApiService;
import huajiu.com.service.RecordService;
import huajiu.com.vo.ApiRequestVo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ItBotPjq
 *2018年1月19日
 */
@RestController
@RequestMapping(value = "/api/")
public class ApiController {
	
	@Autowired
	private ApiService apiService;
	
	@Autowired
	private RecordService recordService;
	
	@ResponseBody
	@RequestMapping(value="cor.do",method=RequestMethod.POST)
	public CdResult corApi(HttpServletRequest request,@RequestBody ApiRequestVo params){
		
		return apiService.getCorInterface( params.getMid(), params.getSign(), params.getTimestamp(),params.getBase64(),params.getExt());
		
	}
	
	@ResponseBody
	@RequestMapping(value="ton.do",method=RequestMethod.POST)
	public CdResult tonApi( @RequestBody ApiRequestVo params){
		
		return apiService.getTonInterface( params.getMid(), params.getSign(),  params.getTimestamp(),params.getBase64(),params.getExt());
		
	}
	
	@ResponseBody
	@RequestMapping(value="emo.do",method=RequestMethod.POST)
	public CdResult emoApi( @RequestBody ApiRequestVo params){
		
		return apiService.getEmoInterface( params.getMid(), params.getSign(), params.getTimestamp(),params.getBase64(),params.getExt());
		
	}
	
	@ResponseBody
	@RequestMapping(value="api/img.do",method=RequestMethod.POST)
	public String  imgApi(HttpServletRequest request) throws FileUploadException, IOException{
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);//判断是否是表单文件类型
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload sfu = new ServletFileUpload(factory);
		String dirFile = "D:/home/wechat/";
		File file = new File(dirFile);
 		if(!file.exists()){
 			file.mkdir();
 		}
		List items = sfu.parseRequest(request);//从request得到所有上传域的列表
		for(Iterator iter = items.iterator();iter.hasNext();){
			FileItem fileitem =(FileItem) iter.next();
				if(!fileitem.isFormField() && fileitem != null){//判读不是普通表单域即是file 操作 fileitem文件步骤，可以获取大小、路径
					System.out.println(fileitem.getSize());
					//File file = new File(fileitem.getFieldName());
					System.out.println(file.exists());
					System.out.println(fileitem.getName());
					InputStream inputStream = fileitem.getInputStream();
					byte[] b = new byte[1024];
					inputStream.read(b);
					String string =new String(b);
					System.out.println(string);
					//InputStream inputStream = new FileInputStream(fileitem.getFieldName());
					BufferedInputStream in = new BufferedInputStream(fileitem.getInputStream());
					BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(new File(dirFile + "\\"
					+ fileitem.getName())));
					
					Streams.copy(in, out, true);
					out.close();
					in.close();
				}
		}
		
		return "Ok";
	}

}
